package com.black.play.entity;


import com.black.play.component.SudokuWindow;
import com.black.play.listener.SudokuKeyListener;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.geom.RoundRectangle2D;
import java.util.Objects;

import static com.black.play.entity.Grid.NODE_SIZE;

/**
 * 单元格类
 *
 * @author ylx
 */
public class Cell extends JTextField
{
    // 横坐标
    private final int x;
    // 纵坐标
    private final int y;
    private Color background;
    // 值
    private CellValue value;
    public static final Font FONT = new Font("Arial", Font.BOLD, 20);

    public Cell(int x, int y)
    {
        this.x = x;
        this.y = y;
        this.background = Color.WHITE;
        setOpaque(false); // 设置为透明，以便我们自己绘制背景
        setPreferredSize(NODE_SIZE);
        addKeyListener(new CellListener());
        // 允许键盘事件聚焦
        setFocusable(true);
        requestFocusInWindow();
    }

    public CellValue getValue()
    {
        return value;
    }

    public void setValue(int value)
    {
        this.value = CellValue.values()[value];
    }

    public void setValue(CellValue value)
    {
        this.value = value;
    }

    public boolean isEmpty()
    {
        return value == CellValue.EMPTY;
    }

    @Override
    public int hashCode()
    {
        return Objects.hash(x, y);
    }

    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        Graphics2D graphics2D = (Graphics2D) g;
        // 绘制圆角边界
        RoundRectangle2D.Float border = new RoundRectangle2D.Float(0, 0, NODE_SIZE.width, NODE_SIZE.height, 0, 0);
        graphics2D.setColor(Color.CYAN);
        graphics2D.setStroke(new BasicStroke(3));
        graphics2D.draw(border);
        // 填充网格
        RoundRectangle2D.Float fill = new RoundRectangle2D.Float(0, 0, NODE_SIZE.width - 2, NODE_SIZE.height - 2, NODE_SIZE.width >> 2, NODE_SIZE.height >> 2);
        graphics2D.setColor(background);
        graphics2D.fill(fill);
        // 点开或者右键标记时绘制文字
        Color fontColor = value.fontColor;
        String text = value.text;
        graphics2D.setColor(fontColor);
        graphics2D.setFont(FONT);
        FontMetrics fm = graphics2D.getFontMetrics();
        int x = (getWidth() - fm.stringWidth(text)) / 2;
        int y = (getHeight() - fm.getHeight()) / 2 + fm.getAscent();
        graphics2D.drawString(text, x, y);
    }

    private class CellListener extends SudokuKeyListener
    {
        @Override
        public void keyPressed(KeyEvent e)
        {
            int keyCode = e.getKeyCode();
            switch (keyCode)
            {
                case 49:
                case 97:
                    value = CellValue.NUMBER_1;
                    break;
                case 50:
                case 98:
                    value = CellValue.NUMBER_2;
                    break;
                case 51:
                case 99:
                    value = CellValue.NUMBER_3;
                    break;
                case 52:
                case 100:
                    value = CellValue.NUMBER_4;
                    break;
                case 53:
                case 101:
                    value = CellValue.NUMBER_5;
                    break;
                case 54:
                case 102:
                    value = CellValue.NUMBER_6;
                    break;
                case 55:
                case 103:
                    value = CellValue.NUMBER_7;
                    break;
                case 56:
                case 104:
                    value = CellValue.NUMBER_8;
                    break;
                case 57:
                case 105:
                    value = CellValue.NUMBER_9;
                    break;
                case KeyEvent.VK_SPACE:
                case KeyEvent.VK_ESCAPE:
                    super.keyPressed(e);
                    break;
                default:
                    value = CellValue.EMPTY;
            }
            SudokuWindow window = SudokuWindow.getInstance();
            window.startTimerIfNotRun();
            if (window.isError(x, y, value))
            {
                background = Color.RED;
                repaint();
            }
            else
            {
                background = Color.WHITE;
                repaint();
                window.judgeSuccess();
            }
        }
    }
}
